const fspro = require('fs').promises;
onst fsExtra = require('fs-extra');
let fs = require('fs');
const path = require('path');
// var images = require("images");
const compress_images = require("compress-images");

const fileHelper = {
  // readFile: (path) => {
  //   return fspro.readFile(path, { encoding: 'utf8'})
  // },
  // writeFile: (path, content) => {
  //   return fspro.writeFile(path, content, { encoding: 'utf8'})
  // },
  // renameFile: (path, newPath) => {
  //   return fspro.rename(path, newPath)
  // },
  // deleteFile: (path) => {
  //   return fspro.unlink(path)
  // },
//   copyFile:(sourcePath, targetPath) => { 
//     cosnt self = this;
//     const fromPath = path.resolve(sourcePath)
//     const toPath = path.resolve(targetPath)
//     fs.access(toPath, (err) => { // 访问权限
//         if (err) { 
//             fs.mkdirSync(toPath);// 同步创建目录
//         }
//     })
//     // 读取目录
//     fs.readdir(fromPath, (err, paths) => { 
//         if (err) { 
//             console.log(err)
//             return
//         }
//         paths.forEach((item) => { 
//             const newFromPath = path.resolve(fromPath + '/' + item)
//             const newToPath = path.resolve(toPath + '/' + item)
//             fs.stat(newFromPath, (err, stat) => { // 拿到文件类型
//                 if (err) return
//                 if (stat.isFile()) { 
//                     // 同步复制src到dest. 默认情况下，dest如果已存在则被覆盖。返回undefined。Node.js 不保证复制操作的原子性。如果在打开目标文件进行写入后发生错误，Node.js 将尝试删除目标。
//                     fs.copyFileSync(newFromPath, newToPath)
//                     console.log(newToPath)
//                 }
//                 if (stat.isDirectory()) { 
//                   self.copyFile(newFromPath, newToPath)
//                 }
//             })
//         })
//     })
// },
  // imageMin:(path) =>{
  //   // nodejs  用可以，但是   electron用就不行
  //   let p = __dirname +'\\2121.png';
  //   console.log('21212121',p);
  //   // if(path){
  //   //   return;
  //   // }
  //   // //支持png/jpeg/gif三种格式输入。目前支持设置JPG输出图像质量   妈的不支持jpg
  //   //     images(p).save(p, {
  //   //     quality : 50                    //保存图片到文件,图片质量为50
  //   // });
  // },
  copy:(sourcePath,targetPath)=>{
    return fs.copy(sourcePath, targetPath);
  },
  imageMin2:(directoryPath)=>{
    // let   INPUT_path_to_your_images = 'D:/personal/electron-demo/electron-react-start/imgText/**/*.{jpg,JPG,jpeg,JPEG,png,svg,gif}';  // 是ok的
    // let   INPUT_path_to_your_images = 'imgText/**/*.{jpg,JPG,jpeg,JPEG,png,svg,gif}';// 相对路径也是ok的
    // D:\textMinimg\弹窗wap
    // let OUTPUT_path = __dirname +'/';
    // console.log('21212121',__dirname);
    // let path_tem = directoryPath; //  'D:/textMinimg/弹窗wap';
    return new Promise((resolve, reject) => {
      if (!fsbase.existsSync(directoryPath)) {
        console.log('路径不存在');
        return;
      }
      let INPUT_path_to_your_images = directoryPath + '/**/*.{jpg,JPG,jpeg,JPEG,png,svg,gif}';
      let OUTPUT_path = directoryPath +'/Cache/';
      compress_images(INPUT_path_to_your_images, OUTPUT_path, { compress_force: false, statistic: true, autoupdate: true }, false,
                        { jpg: { engine: "mozjpeg", command: ["-quality", "78"] } },// 调到80和这个https://tinypng.com/ 基本是接近的，我们这里调的比他小一点 ，出来的文件会小5-8kb
                        { png: { engine: "pngquant", command: ["--quality=20-50", "-o"] } },
                        { svg: { engine: "svgo", command: "--multipass" } },
                        { gif: { engine: "gifsicle", command: ["--colors", "64", "--use-col=web"] } },
          function (error, completed, statistic) {
            console.log("-------------");
            console.log(error);
            console.log(completed);
            console.log(statistic);
            console.log("-------------");
          }
        );
    })
  }
}

export default fileHelper

